﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;

using YLPublicService.TransferModel;
using YLServiceCLient.YLAdminClient;
using YLServerInterface.ServerConfig;

namespace YLServerInterface.FTP
{
    //delegate void BeforeTransferNextEventHandler(string fileName, int index, int count);
    //delegate void OnTransferFileErrorEventHandler(string fileName, int index);
    /// <summary>
    /// 执行下一次传输时被触发
    /// </summary>
    /// <param name="preFileName"></param>
    /// <param name="precent">当前工作进度</param>
    delegate void TransfefMoveNext(string preFileName, double precent);
    delegate void TransferFileError(string fileName);

    internal class ClientAdapter
    {
        AdminServiceClient svcClient;
        FTP ftp;
        FtpServerInfo info;
        public ClientAdapter(bool regetFtpServerInfo)
        {
            svcClient = AdminServiceClient.GetServiceClient();
            info = svcClient.RequestFtpServerInfo();
            ftp = new FTP(info.IPV4, info.ClientLoginName, info.ClientPassWord);
        }


        /// <summary>
        /// 提供并行模型使用
        /// </summary>
        public event TransfefMoveNext OnTransfefMoveNext;

        ///bool参数是 代表是否有错
        public event Action<bool> OnTransferFinish;
        public event Action<string> OnTransferError;

        //当前工作进度
        int currentValue;
        //完成工作时的进度
        int finalValue;

        private List<Tuple<WorkItem, string>> errorUploadWorkItems;

        public bool UploadFile(string remoteDir, string file)
        {
            var fileName = Common.getFileName(file);
            try
            {
                ftp.UploadFile(Common.conbindPath(remoteDir, fileName), file);
            }
            catch (Exception e)
            {
                return false;
            }
            return true;
        }
        public bool UploadFiles(string remoteDir, string[] files)
        {
            var ret = from file in files
                      select Tuple.Create(file, Common.conbindPath(remoteDir, Common.getFileName(file)));
            ftp.UploadFiles(ret);
            return true;
        }
        public bool DownloadFiles(string remoteDir, string localDir)
        {
            bool hasErr = false;
            try
            {
                ftp.DownloadFiles(remoteDir, localDir);
            }
            catch (Exception err)
            {
                hasErr = true;
                if (OnTransferError != null)
                {
                    OnTransferError(err.Message);
                }
                else
                {
                    throw;
                }
            }
            if (OnTransferFinish != null)
            {
                OnTransferFinish(hasErr);
            }
            return hasErr;
        }
    }
}
